package priv.bruce.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mysql.fabric.xmlrpc.base.Data;

/**
 * JDBC：java database connection = java数据库连接对象
 * jdbc是一套接口
 * 由数据库厂商提供具体实现
 * 	- mysql
 * 	- oracle
 * 	- sqlite
 * 	- hive
 * @author Bruce
 *
 */
public class JDBCUtil {

//	private static final String DRIVER = MySQLMessages.getString("database.driver"); //$NON-NLS-1$
//	private static final String URL = MySQLMessages.getString("database.url"); //$NON-NLS-1$
//	private static final String USER = MySQLMessages.getString("database.user"); //$NON-NLS-1$
//	private static final String PASSWORD = MySQLMessages.getString("database.password"); //$NON-NLS-1$
	
	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String URL = "jdbc:mysql://lsf.liuxiaoyi.com.cn:3306/test?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull";
	private static final String USER = "bruce";
	private static final String PASSWORD = "123";
	static{
		//加载驱动类
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static <T> List<T> query(String sql,HandlerSet hs,Object...args) throws Exception {
		//1.获取连接
		Connection conn = getConn();
		
		//2.获取sql执行对象
		PreparedStatement pre = conn.prepareStatement(sql);
		
//		pre.setString(1, "804");//1代表第一个问号。
//		pre.setObject(1, 804);
		for (int i = 0; i < args.length; i++) {
			pre.setObject(i+1, args[i]);
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
		System.out.println(pre);
		//3.操作sql执行对象获取结果集
		ResultSet rs = pre.executeQuery();
//		System.out.println("结果集："+rs);
		//4.处理结果集
		List<T> list = new ArrayList<>();
		while(rs.next()) {
//			System.out.println(rs.getObject(1));//所有都是从0开始，唯独这里从1开始，1表示第一列
//			System.out.println(rs.getObject("sname"));
			
			T t = (T) hs.handler(rs);
//			student.setSno(rs.getString("sno"));
//			student.setSname(rs.getString("sname"));
//			student.setSsex(rs.getString("ssex"));
//			student.setSbirthday(rs.getDate("sbirthday"));
//			student.setClazz(rs.getString("class"));
			list.add(t);
		}
		
		//5.关闭资源
		closeSource(conn, pre, rs);
//		System.out.println("实体对象："+list);
		return list;
	}
	
	public static int update(String sql,Object...args) throws Exception {
		//1.获取连接
		Connection conn = getConn();
		
		//2.获取sql执行对象
		PreparedStatement pre = conn.prepareStatement(sql);
		
//		pre.setString(1, "804");//1代表第一个问号。
//		pre.setObject(1, 804);
		for (int i = 0; i < args.length; i++) {
			pre.setObject(i+1, args[i]);
		}
		System.out.println(pre);
		int result = pre.executeUpdate();
		
		//5.关闭资源
		closeSource(conn, pre, null);
		
		return result;
	}

//	private static void closeSource(Connection conn, PreparedStatement pre, ResultSet rs) throws SQLException {
//		//5.关闭资源
//		if (rs!=null) {
//			rs.close();
//		}
//		if (pre!=null) {
//			pre.close();
//		}
//		if (conn!=null) {
//			conn.close();
//		}
//	}
	//要循环，循环元素必须先是同一类型
	//而AutoCloseable是Connection，PreparedStatement和ResultSet的共同父类
	//父类接收子类对象
	//...acs表示任意长度的参数列表
	private static void closeSource(AutoCloseable...acs) throws Exception {
		//5.关闭资源
		for (AutoCloseable autoCloseable : acs) {
			if (autoCloseable != null) {
				autoCloseable.close();
			}
		}
	}

	private static Connection getConn() throws SQLException {
		//1.获取连接
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		return conn;
	}
}
